package com.medical.admin.sys.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.admin.sys.common.dto.InformDto;
import com.medical.admin.sys.entity.Inform;
import com.medical.admin.sys.mapper.InformMapper;
import com.medical.admin.sys.service.InformService;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.uitils.BeanPageUtils;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Supplier;

/*
 * @Date : 2024/4/28 下午3:47
 * @Created by Lang
 * @ProjectName : medical-appointment-registration
 */
@Service
public class InformServiceImpl implements InformService {

    @Resource
    private InformMapper informMapper;

    @Override
    public Integer addInform(InformDto informDto) {
        Inform inform = BeanConvertUtils.convertTo(informDto, Inform::new);
        if (ObjectUtil.isNotEmpty(informMapper.selectByInformHeadline(informDto.getMarInformHeadline()))) {
            throw new ServiceException(ResponseCode.INFORM_HEADLINE_IS_HAVE);
        }
        inform.setMarInformId(IdUtil.getSnowflakeNextId());
        return informMapper.insert(inform);
    }

    @Override
    public Integer modInform(InformDto informDto) {
        Inform inform = BeanConvertUtils.convertTo(informDto, Inform::new);
        // 查询是否存在相同的标题
        LambdaQueryWrapper<Inform> ne = new LambdaQueryWrapper<Inform>().eq(Inform::getMarInformHeadline, inform.getMarInformHeadline()).ne(Inform::getMarInformId, inform.getMarInformId());
        if (ObjectUtil.isNotEmpty(informMapper.selectOne(ne))) {
            throw new ServiceException(ResponseCode.INFORM_HEADLINE_IS_HAVE);
        }
        return informMapper.updateById(inform);
    }

    @Override
    public InformDto selectById(Long informId) {
        Inform inform = informMapper.selectById(informId);
        return BeanConvertUtils.convertTo(inform, InformDto::new);
    }

    @Override
    public Integer delById(Long informId) {
        return informMapper.delById(informId);
    }

    @Override
    public Integer delBatch(List<Long> informIds) {
        return informMapper.modStatusByBatch(informIds);
    }

    @Override
    public Integer modStatus(Long informId, Integer informStatus) {
        LambdaUpdateWrapper<Inform> eq = new LambdaUpdateWrapper<Inform>().set(Inform::getMarInformStatus, informStatus)
                .eq(Inform::getMarInformId, informId);
        return informMapper.update(eq);
    }

    @Override
    public Page<InformDto> list(String informName, Integer pageNum, Integer pageSize) {
        Page<Inform> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Inform> wrapper = new LambdaQueryWrapper<>();
        if (informName != null && !informName.isEmpty()) {
            wrapper.like(Inform::getMarInformHeadline, informName);
        }
        Page<Inform> informPage = informMapper.selectPage(page, wrapper);
        Page<InformDto> informDtoPage = new Page<>();

        Supplier<InformDto> targetSupplier = InformDto::new;
        return BeanPageUtils.convertPage(informPage, informDtoPage, targetSupplier);
    }
}
